package pl.prv.polanie.client.widgets.schedule.custom;

import java.util.ArrayList;
import java.util.List;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSPreferenceTypeDTO;
import pl.prv.polanie.client.dto.CSVPreferenceTypeDTO;
import pl.prv.polanie.client.model.Cache;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 * Created: May 16, 2008
 * 
 * Kontrolka listujaca dostepne preferenceType i pozwalajaca na wybor jednego z nich.
 */
public class PreferenceTypeChooser extends Composite {
	
	private static final String STYLE_PREFERENCE_TYPE_CHOOSE__ITEM = "my-PreferenceTypeChooser-item";
	private static final String STYLE_PREFERENCE_TYPE_CHOOSE__ITEM_HOVERED = "my-PreferenceTypeChooser-itemHovered";

	private List preferenceTypesDTOs = new ArrayList();
	
	private CSPreferenceTypeDTO selectedItemDTO = null;
	
	private VerticalPanel mainVP = new VerticalPanel();
	
	public PreferenceTypeChooser() {
		
		mainVP.setBorderWidth(0);
		mainVP.setSpacing(0);
		
		initWidget(mainVP);
		
		
		
		CSPreferenceTypeDTO[] pts = Cache.getAllPreferenceTypes();
		Planner.log("PreferenceTypeChooser pobrano ptsow " + pts.length);
		for (int i = 0; i < pts.length; i++) {
			//->cs
			//->csv

			CSPreferenceTypeDTO cspt = new CSPreferenceTypeDTO(pts[i].getId(), pts[i].getName(), pts[i].getColor());
			CSVPreferenceTypeDTO csvpt = new CSVPreferenceTypeDTO(cspt.getId(), cspt.getName(), cspt.getColor());
			
			addPreferenceType(cspt, csvpt);
		}
		
	}
	
	private void addPreferenceType(CSPreferenceTypeDTO dto, CSVPreferenceTypeDTO vdto) {
		String htmlCode = "<table><tr><td><div style='width:16px; height: 16px; background-color:" + vdto.getColor() + ";' >" +
			" </div></td><td> " + vdto.getName() + "</td></tr></table>";
		
		preferenceTypesDTOs.add(dto);
		
		final HTML ptHTML = new HTML(htmlCode, false);
		ptHTML.setStyleName(STYLE_PREFERENCE_TYPE_CHOOSE__ITEM);
		
		ptHTML.addMouseListener(new MouseListenerAdapter() {
//			@Override
			public void onMouseEnter(Widget sender) {
				super.onMouseEnter(sender);
				ptHTML.addStyleName(STYLE_PREFERENCE_TYPE_CHOOSE__ITEM_HOVERED);
			}
//			@Override
			public void onMouseLeave(Widget sender) {
				super.onMouseLeave(sender);
				ptHTML.removeStyleName(STYLE_PREFERENCE_TYPE_CHOOSE__ITEM_HOVERED);
			}
//			@Override
			public void onMouseDown(Widget sender, int x, int y) {
				super.onMouseDown(sender, x, y);
				
				int selectedItemIndex = mainVP.getWidgetIndex(ptHTML);
				selectedItemDTO = (CSPreferenceTypeDTO)preferenceTypesDTOs.get(selectedItemIndex);
				
				Planner.log("PTChooser wybrano " + selectedItemDTO.toString());
				runOnSelectedItem();
			}
		});
		
		mainVP.add(ptHTML);
	}
	
	/**
	 * Funkcja wywolywana w momencie wybrania ktoregos z elementow.
	 * Tutaj kontrolka powinna byc chowana. 
	 */
	protected void runOnSelectedItem() {}

	/**
	 * Zwraca dto skojarzone z wybranym elementem w kontrolce.
	 * @return
	 */
	public CSPreferenceTypeDTO getSelectedItemDTO() {
		return selectedItemDTO;
	}
	
}
